package com.wl.rjwmc.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.rjwmc.DTO.SetmealDto;
import com.wl.rjwmc.Dao.SetmealDao;
import com.wl.rjwmc.DoMain.Setmeal;
import com.wl.rjwmc.DoMain.SetmealDish;
import com.wl.rjwmc.Service.SetmealDishService;
import com.wl.rjwmc.Service.SetmealService;
import com.wl.rjwmc.Utools.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐基本信息
        this.save(setmealDto);

        // 保存套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        // 查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if(count > 0){
            throw new CustomException("套餐正在售卖中，无法删除");
        }

        // 可以删除，先删除套餐表中的数据
        this.removeByIds(ids);

        // 删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapper);
    }

    @Override
    @Transactional
    public SetmealDto setmealBackData(Long id) {
        //创建dto对象用于赋值后返回
        SetmealDto setmealDto = new SetmealDto();

        //根据ID查询setmeal
        Setmeal setmeal = this.getById(id);

        //对象拷贝，将查询出来的setmeal给dto赋值
        BeanUtils.copyProperties(setmeal,setmealDto);

        //根据setmealId查询出setmeal_dish表中的所有对应数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        //将查询出来的套餐菜品对应关系列表数据添加入dto中
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    @Transactional
    public void setmealUpdate(SetmealDto setmealDto) {
        //向setmeal更新数据
        this.updateById(setmealDto);
        //向setmeal_dish表更新数据
        List<SetmealDish> list = setmealDto.getSetmealDishes();

        //因为套餐菜品关系表中没有套餐的ID，需要手动设置
        Long id = setmealDto.getId();
        list.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());

        //更新setmeal_dish'表策略：先删除原来内容，后添加新内容即可
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishService.remove(queryWrapper);
        //添加
        setmealDishService.saveBatch(list);
    }

}
